package com.samehope.ar.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.samehope.ar.constant.DelFlagEnum;
import com.samehope.ar.dto.ProjectTeamAddParam;
import com.samehope.ar.dto.ProjectTeamUpdateParam;
import com.samehope.ar.entity.ProjectTeam;
import com.samehope.ar.mapper.ProjectTeamMapper;
import com.samehope.ar.service.IProjectTeamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.samehope.ar.util.SessionUtil;
import com.samehope.ar.util.TreeUtils;
import com.samehope.ar.vo.FormSelectVO;
import com.samehope.ar.vo.ProjectTeamDataVO;
import com.samehope.ar.vo.TreeDataVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 项目团队 服务实现类
 * </p>
 *
 * @author Zhang Luo
 * @since 2019-12-23
 */
@Service
public class ProjectTeamServiceImpl extends ServiceImpl<ProjectTeamMapper, ProjectTeam> implements IProjectTeamService {

    /**
     * 获取项目团队成员信息
     * @return
     */
    @Override
    public List<ProjectTeamDataVO> findByProject(){
        Long companyId = SessionUtil.getCompanyId();
        Long projectId = SessionUtil.getProjectId();

        return this.baseMapper.findByProject(companyId, projectId);
    }

    @Override
    public void add(ProjectTeamAddParam param){
        Long companyId = SessionUtil.getCompanyId();
        Long projectId = SessionUtil.getProjectId();

        //判断项目团队名称是否存在
        ProjectTeam projectTeam = this.baseMapper.findByProjectTeamName(companyId, projectId, param.getTeamName(), null);
        if (projectTeam != null) {
            throw new IllegalArgumentException("项目团队名称已存在");
        }
        //保存
        ProjectTeam team = new ProjectTeam();
        team.setCompanyId(companyId);
        team.setProjectId(projectId);
        team.setTeamName(param.getTeamName());
        team.setCreateBy(SessionUtil.getUserId());
        team.setUpdateBy(SessionUtil.getUserId());
        this.save(team);
    }

    @Override
    public void update(ProjectTeamUpdateParam param){
        Long companyId = SessionUtil.getCompanyId();
        Long projectId = SessionUtil.getProjectId();

        //判断项目团队是否存在
        QueryWrapper<ProjectTeam> wrapper = new QueryWrapper<>();
        ProjectTeam wrapperData = new ProjectTeam();
        wrapperData.setId(param.getTeamId());
        wrapperData.setCompanyId(companyId);
        wrapperData.setProjectId(projectId);
        wrapper.setEntity(wrapperData);
        ProjectTeam data = this.getOne(wrapper);
        if(null == data){
            throw new IllegalArgumentException("项目团队不存在");
        }

        //判断项目团队名称是否存在
        ProjectTeam projectTeam = this.baseMapper.findByProjectTeamName(companyId, projectId, param.getTeamName(), param.getTeamId());
        if (projectTeam != null) {
            throw new IllegalArgumentException("项目团队名称已存在");
        }

        //更新
        ProjectTeam team = new ProjectTeam();
        team.setId(param.getTeamId());
        team.setTeamName(param.getTeamName());
        team.setUpdateBy(SessionUtil.getUserId());
        this.updateById(team);
    }

    @Override
    public void del(Long teamId){
        Long companyId = SessionUtil.getCompanyId();
        Long projectId = SessionUtil.getProjectId();

        //判断项目团队是否存在
        QueryWrapper<ProjectTeam> wrapper = new QueryWrapper<>();
        ProjectTeam wrapperData = new ProjectTeam();
        wrapperData.setId(teamId);
        wrapperData.setCompanyId(companyId);
        wrapperData.setProjectId(projectId);
        wrapper.setEntity(wrapperData);
        ProjectTeam data = this.getOne(wrapper);
        if(null == data){
            throw new IllegalArgumentException("项目团队不存在");
        }

        ProjectTeam projectTeam = new ProjectTeam();
        projectTeam.setId(teamId);
        projectTeam.setUpdateBy(SessionUtil.getUserId());
        projectTeam.setDelFlag(DelFlagEnum.DELETED.getCode());
        this.updateById(projectTeam);
    }

    /**
     * 获取团队成员树结构
     * @return
     */
    @Override
    public List<TreeDataVO> getTree() {
        List<TreeDataVO> tree = this.baseMapper.getTreeByProject(SessionUtil.getProjectId());

        // 有一部分人, 不再团队中, 目前先放置-1, 为其他组, 默认的
        TreeDataVO elseTeam = new TreeDataVO();
        elseTeam.setKey("-1");
        elseTeam.setValue("-1");
        elseTeam.setTitle("其他");

        tree.get(0).getChildren().add(elseTeam);
        TreeUtils.setLevel(tree, 1);
        TreeUtils.setId(tree);
        return tree;
    }

    /**
     * 获取团队下拉框数据
     * @return
     */
    @Override
    public List<FormSelectVO> listForSelect() {
        return this.baseMapper.listForSelectByProject(SessionUtil.getProjectId());
    }
}
